class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        res = 0
        low, high = prices[0], prices[0]
        i = 1
        while i < len(prices):
            if prices[i] > high:
                high = prices[i]
            if low > prices[i]:
                profit = high - low
                if profit > res:
                    res = profit
                low = prices[i]
                high = prices[i]
            i += 1
        profit = high - low
        if profit > res:
            res = profit
        return res
